package com.longdaji.decoration.ui.activitiesOfMine.refund;

import android.text.TextUtils;

import com.longdaji.decoration.Constants;
import com.longdaji.decoration.base.RxPresenter;
import com.longdaji.decoration.bean.Account;
import com.longdaji.decoration.bean.Orders;
import com.longdaji.decoration.bean.RefundGoods;
import com.longdaji.decoration.data.DataManager;
import com.longdaji.decoration.data.network.CommonSubscriber;
import com.longdaji.decoration.ui.activitiesOfMine.refund.RefundContract;
import com.longdaji.decoration.utils.LogUtil;
import com.longdaji.decoration.utils.RxUtil;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by Kanade on 2018/3/8 0008.
 */

public class RefundPresenter extends RxPresenter<RefundContract.View> implements RefundContract.Presenter {
    private DataManager mDataManager;
    private Account account;

    @Inject
    public RefundPresenter(DataManager dataManager) {
        this.mDataManager = dataManager;
        account = Account.getInstance();
    }

    @Override
    public void initData() {
//        doGetOrder();
        doGetRefundGoods();
    }

    private void doGetRefundGoods() {
        addSubscribe(mDataManager.doGetRefundGoodsApi(Account.getInstance().getUserid())
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .compose(RxUtil.<List<RefundGoods>>handleResult())
                .subscribeWith(new CommonSubscriber<List<RefundGoods>>() {
                    @Override
                    public void onSuccess(List<RefundGoods> refundGoodsList) {
                        LogUtil.i("mytest", "success doGetRefundGoodsApi");
                        mView.showRefundGoods(refundGoodsList);
                    }

                    @Override
                    public void onFailed(String msg) {
                        LogUtil.i("mytest", "fail doGetRefundGoodsApi:" + msg);
                    }
                }));
    }

//    private void doGetOrder() {
//        final List<Orders.OrdersBean.OrderBean> refundOrderList = new ArrayList<>();
//        addSubscribe(mDataManager.doGetOrderApi(account.getUserid(), null, null)
//                .subscribeOn(Schedulers.io())
//                .observeOn(AndroidSchedulers.mainThread())
//                .compose(RxUtil.<Orders>handleResult())
//                .subscribeWith(new CommonSubscriber<Orders>() {
//                    @Override
//                    public void onSuccess(final Orders orders) {
//                        LogUtil.d("succeed : doGetOrder ");
//                        Observable.create(new ObservableOnSubscribe<List<Orders.OrdersBean.OrderBean>>() {
//                            @Override
//                            public void subscribe(ObservableEmitter<List<Orders.OrdersBean.OrderBean>> e) throws Exception {
//                                for (Orders.OrdersBean.OrderBean orderBean : orders.getOrders().getOrder()) {
//                                    // TODO: 2018/4/9 0009 这里只是暂定为5，等接口改出来再看
//                                    if (Constants.ORDER_FINISHED == orderBean.getOrderStatus()) {
//                                        refundOrderList.add(orderBean);
//                                    }
//                                }
//                                e.onNext(refundOrderList);
//                            }
//                        }).subscribeOn(Schedulers.io())
//                                .observeOn(AndroidSchedulers.mainThread())
//                                .subscribe(new Consumer<List<Orders.OrdersBean.OrderBean>>() {
//                                    @Override
//                                    public void accept(List<Orders.OrdersBean.OrderBean> pendingDeliveryOrderList) throws Exception {
//                                        mView.showRefundOrders(pendingDeliveryOrderList);
//                                    }
//                                });
//
//                    }
//
//                    @Override
//                    public void onFailed(String msg) {
//                        LogUtil.d("failed : " + msg);
//                    }
//                }));
//    }
}
